import { obj_str, is_empty, d_sort, uDb, cline, ck_sn, ck_empty, add_ck_col, reload_col, loc } from 'sui';

const fix_col = () => {
    return [
        {sn: "sn", name: loc("l_132"), tp: "string"},
        {sn: "name", name: loc("l_412"), tp: "string"},
    ];
};

const tech_mod = (mod) => {
    if (mod === "short") {
        return "tshort";
    }

    return "tech";
};

const psn_mod = (mod) => {
    if (mod === "short") {
        return "pshort";
    }

    return "psn";
};

const load_tech_col = (setCol, mod) => {
    const fix = fix_col();
    reload_col(null, v => setCol(v), tech_mod(mod), fix);
};

const get_ck = (cur, main, col) => {
    const ret = {
        sn : v => ck_sn(v, cur, main),
        name : v => ck_empty(v),
    };

    add_ck_col(ret, col);

    return ret;
};

const reload = (hDone, mod, setMain) => {
    uDb(hDone, "tech/list", { line: cline() },
        r => {
            let ret = null;

            if (mod === "angle") {
                ret = r.filter(d => !d.tp || d.tp === mod);
            } else {
                ret = r.filter(d => d.tp === mod);
            }

            if (ret) {
                d_sort(ret, "sn");
                ret.forEach((d, i) => d["index"] = i + 1);
            }

            setMain(ret);
        }, e => setMain(null));
};

const get_title = (mod) => {
    const ret = {
        "angle": loc("l_898"),
        "short": loc("l_899"),
    };

    return ret[mod];
};

const load_station = (setStation) => {
    uDb(null, "station/list",
        {
            line: cline(),
        },
        r => {
            const tmp = r.map(d => {
                return {sn: d.sn, name: d.name};
            });

            setStation(tmp);
        },
        e => setStation(null));
};

const load_supply = (mapSupply, tp="supply", gtp="group") => {
    const cmd = { line: cline() };

    uDb(null, `file/list/${tp}`, cmd,
        supply => {
            const map = {};
            supply.forEach(d => map[d.sn] = d.name);

            uDb(null, `file/list/${gtp}`, cmd,
                group => {
                    const ret = {};

                    group.forEach(d => {
                        const sn = obj_str(d, "sn");
                        if (!sn) {
                            return;
                        }

                        const tmp = [];

                        if (d.user) {
                            d.user.forEach(t => {
                                const v = map[t];
                                if (!v) {
                                    return;
                                }

                                tmp.push({ sn: t, name: v });
                            });
                        }

                        if (is_empty(tmp)) {
                            return;
                        }

                        ret[sn] = tmp;
                    });

                    mapSupply(ret);
                },
                e => mapSupply(null));
        },
        e => mapSupply(null));
};

const load_stuff = (mapStuff) => load_supply(mapStuff, "stuff", "gstuff");

export {load_supply, load_station, get_title, load_stuff, psn_mod, get_ck, load_tech_col, tech_mod, fix_col, reload};
